#include <cstdio>

typedef long long ll;
const int N = 100100;
int n, cnt, tot;
int a[N], heads[N], rt[N];

struct Edge {
  int nxt, v;
} e[N << 1];

struct Node {
  int ls, rs, mmax;
  ll sumid;
} tr[N << 6];

inline void add(int u, int v) {
  e[++cnt].v = v;
  e[cnt].nxt = heads[u];
  heads[u] = cnt;
}

inline void pushup(int x) {
  if (tr[tr[x].ls].mmax == tr[tr[x].rs].mmax)
    tr[x].mmax = tr[tr[x].ls].mmax, tr[x].sumid = tr[tr[x].ls].sumid + tr[tr[x].rs].sumid;
  else if (tr[tr[x].ls].mmax > tr[tr[x].rs].mmax)
    tr[x].mmax = tr[tr[x].ls].mmax, tr[x].sumid = tr[tr[x].ls].sumid;
  else
    tr[x].mmax = tr[tr[x].rs].mmax, tr[x].sumid = tr[tr[x].rs].sumid;
}

void modify(int &x, int l, int r, int pos) {
  if (!x) {
    x = ++tot;
  }
  if (l == r) {
    tr[x].mmax = 1, tr[x].sumid = l;
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    modify(tr[x].ls, l, mid, pos);
  else
    modify(tr[x].rs, mid + 1, r, pos);
  pushup(x);
}

int merge(int x, int y, int l, int r) {
  if (!x) {
    return y;
  }
  if (!y) {
    return x;
  }
  if (l == r) {
    tr[x].mmax += tr[y].mmax;
    tr[x].sumid = l;
    return x;
  }
  int mid = (l + r) >> 1;
  tr[x].ls = merge(tr[x].ls, tr[y].ls, l, mid);
  tr[x].rs = merge(tr[x].rs, tr[y].rs, mid + 1, r);
  pushup(x);
  return x;
}

bool vis[N];
int maxr;
ll ans[N];

void solve(int x) {
  for (int i = heads[x]; i; i = e[i].nxt) {
    int v = e[i].v;
    if (!vis[v]) {
      vis[v] = true;
      solve(v);
      merge(rt[x], rt[v], 1, maxr);
    }
  }
  ans[x] = tr[rt[x]].sumid;
}

inline int max(int a, int b) { return a > b ? a : b; }

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("CF600E.in", "r", stdin);
  freopen("CF600E.out", "w", stdout);
#endif
#endif

  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    maxr = max(maxr, a[i]);
  }
  for (int i = 1; i <= n; ++i) {
    modify(rt[i], 1, maxr, a[i]);
  }
  int x, y;
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  vis[1] = true;
  solve(1);
  for (int i = 1; i <= n; ++i) {
    printf("%lld ", ans[i]);
  }
  return 0;
}